#!/usr/bin/env python
import os
import urllib2, urllib

def fetch(query):
    request = urllib2.Request('http://localhost:5879/query?%s' % urllib.urlencode({'q':query}))
    print "fetching"
    opener = urllib2.build_opener()
    try:
        response = opener.open(request).read()
    except Exception, e:#blindly delete node if exception
        print e
        return ""
    return response

def test_cases():
    try:
        fp = open('test.txt', 'r')
        fo = open('res.txt', 'w')
        lines = fp.readlines()
        fp.close()
        for line in lines:
            fo.writelines('SQL: %s\nResult:%s\n\n' % (line, fetch(line)))
        fo.close()
    except IOError:
        print "Please create cases in test.txt"
        
if __name__ == '__main__':
    test_cases()
        
